﻿using System;
using System.Web;
using Nexia.FluentHtmlElements.Html;

namespace Nexia.FluentHtmlElements.Elements
{
	public class HyperLinkBase<T> : ElementWithBody<T> where T : HyperLinkBase<T>
	{
		public HyperLinkBase()
			: base(HtmlTagNames.A)
		{
		}

		/// <summary>
		/// Set the 'href' attribute.
		/// </summary>
		/// <param name="value">The value of the 'href' attribute.</param>
		public virtual T Href(string value)
		{
			var href = HttpUtility.UrlPathEncode(value);

			Builder.MergeAttribute(HtmlTagAttributes.Href, href, true);
			return (T)this;
		}

		public virtual T Href(Uri uri)
		{
			if (uri != null)
			{
				return Href(uri.ToString());
			}
			return (T) this;
		}

		protected virtual bool HasHref()
		{
			string value;
			if (Builder.Attributes.TryGetValue(HtmlTagAttributes.Href, out value))
			{
				if (!String.IsNullOrEmpty(value))
					return true;
			}

			return false;
		}
	}
}